<template>
    <div class="container">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="120px"
                 class="demo-ruleForm">
            <el-form-item label="选择地区" prop="p1">
                <regionSelect ref="regionSelect" @done="getRegion"></regionSelect>
            </el-form-item>
            <el-form-item label="小区名称" prop="p2">
                <el-input type="text" v-model="ruleForm2.p2" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="简介" prop="p3">
                <el-input type="textarea" v-model="ruleForm2.p3" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="地理位置" prop="p4">
                <markMap ref="markMap" :p="[ruleForm2.p4,ruleForm2.p5]" @point="getPoint"></markMap>
            </el-form-item>
            <template v-if="!this.uid">
                <el-form-item label="初始管理员姓名" prop="p5">
                    <el-input type="text" v-model="ruleForm2.p5" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="昵称" prop="p6">
                    <el-input type="text" v-model="ruleForm2.p6" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="p7">
                    <!--<el-input type="text" v-model="ruleForm2.p7" auto-complete="off"></el-input>-->
                    <el-radio v-model="ruleForm2.p7" label="1">先生</el-radio>
                    <el-radio v-model="ruleForm2.p7" label="2">女士</el-radio>
                </el-form-item>
                <el-form-item label="手机号" prop="p8">
                    <el-input type="text" v-model="ruleForm2.p8" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="登录密码" prop="p9">
                    <el-input type="text" v-model="ruleForm2.p9" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="头像" prop="p10">
                    <img :src="ruleForm2.p10" style="width: 80px;height: 80px;vertical-align: middle;background-color: #eee;">
                    <img src="../../../static/img/plus.png" alt="" @click="upload" style="vertical-align: middle;">
                </el-form-item>
            </template>
            <br>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
                <!--<el-button @click="getCheckedNodes">获取</el-button>-->
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {quillEditor} from 'vue-quill-editor';
    import pub from '../../utils/pub'
    import md5 from 'md5'
    import regionSelect from '@/components/common/region-select'
    import markMap from '@/components/common/mark-map'

    export default {
        data: function () {
            var v1 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入标题'));
                } else if (value < 2 || value.length > 10) {
                    callback(new Error('2-10位'));
                } else {
                    callback();
                }
            };
            return {
                tree: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                arr: [],
                ruleForm2: {
                    p1: '',
                    p2: '',
                    p3: '',
                    p4: '',
                    p5: '',
                    p6: '',
                    p7: '1',
                    p8: '',
                    p9: '',
                    p10: '',
                },
                rules2: {
                    // p1: [
                    //     {required: true, validator: v1, trigger: 'blur'}
                    // ],
                    p1: [
                        {required: true, trigger: 'blur'}
                    ],
                    p2: [
                        {required: true, trigger: 'blur'}
                    ],
                }
            }
        },
        components: {
            quillEditor,
            regionSelect,
            markMap
        },
        props: ['uid'],
        methods: {
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 获取角色信息
            getInfo() {
                if(!this.uid) {
                    this.ruleForm2.p1 = '';
                    this.ruleForm2.p2 = '';
                    return;
                }
                this.loadopen();
                this.config.post('/manager/housinggestate/housinggestate/findById', {id: this.uid}).then(res => {
                    this.loadclose();
                    if (res.data.code === '0') {
                        // console.log(res.data, '获取角色信息');
                        let data = res.data.housinggestate;
                        this.ruleForm2.p2 = data.name;
                        this.$refs.regionSelect.inject(data.provinces_id, data.cities_id, data.areas_id)
                        this.ruleForm2.p3 = data.introduction;
                        this.ruleForm2.p4 = data.longitude;
                        this.ruleForm2.p5 = data.latitude;
                        this.$refs.markMap.setPoint([this.ruleForm2.p4, this.ruleForm2.p5]);
                    } else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                });
            },
            // 获取选择的菜单ids
            getCheckedNodes() {
                // console.log(this.$refs.tree.getCheckedNodes());
                let arr = [];
                this.$refs.tree.getCheckedNodes().forEach(item=>{
                    arr.push(item.id);
                    if(item.pid&&arr.indexOf(item.pid)==-1){
                        arr.push(item.pid);
                    }
                });
                return arr;
            },
            setCheckedKeys(ids) {
                let arr = [];
                this.arr.forEach(it=>{
                    if(it.children&&it.children.length){
                        if(ids.indexOf(it.id)>=0){
                            ids.splice(ids.indexOf(it.id), 1);
                        }
                    }
                });
                this.$refs.tree.setCheckedKeys(ids);
            },
            getRegion(arr){
                this.ruleForm2.p1 = arr;
            },
            getPoint(arr){
                this.ruleForm2.p4 = arr;
            },
            upload(){
                let self = this;
                let el = document.createElement('input')
                el.type = 'file';el.accept="image/*"
                el.onchange = (e)=>{
                    self.ruleForm2.p10 = '';
                    self.loadopen()
                    pub.OssUpload(e.target.files[0], (res)=>{
                        self.loadclose()
                        // console.log(res, '哈哈，我拿到了')
                        self.ruleForm2.p10 = res;
                    })
                };
                el.click();
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let obj = {
                            'h.id': this.uid,
                            'h.provinces_id': this.ruleForm2.p1[0].provinces_id,
                            'h.province': this.ruleForm2.p1[0].province,
                            'h.cities_id': this.ruleForm2.p1[1].cities_id,
                            'h.city': this.ruleForm2.p1[1].city,
                            'h.areas_id': this.ruleForm2.p1[2].areas_id,
                            'h.area': this.ruleForm2.p1[2].area,
                            'h.name': this.ruleForm2.p2,
                            'h.introduction': this.ruleForm2.p3,
                            'h.longitude': this.ruleForm2.p4[0],
                            'h.latitude': this.ruleForm2.p4[1],
                        };
                        if(!this.uid){
                            obj.assign({
                                'u.user_name': this.ruleForm2.p5,
                                'u.user_nick': this.ruleForm2.p6,
                                'u.sex': this.ruleForm2.p7,
                                'u.mobile': this.ruleForm2.p8,
                                'u.login_pwd': md5(this.ruleForm2.p9),
                                'u.head_url': this.ruleForm2.p10,
                            })
                        }
                        // console.log(obj);
                        let url = this.uid ? '/manager/housinggestate/housinggestate/update' : '/manager/housinggestate/housinggestate/add';
                        this.loadopen();
                        this.config.post(url, obj).then(res => {
                            this.loadclose();
                            if (res.data.code === '0') {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                            this.$emit('done', '');
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        },
        mounted() {
        }
    }
</script>
